package com.recommend.controller.user.customer;

import com.recommend.domain.UserDomain;
import com.recommend.dto.customer.CustomerDto;
import com.recommend.service.UserService;
import com.recommend.service.Customer.CustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.Session;

import java.util.List;

/**
 * Created by lw.
 */

@RestController
@RequestMapping("/user/customer")
public class CustomerController {

    private final static Logger logger = LoggerFactory.getLogger(CustomerController.class);

    @Resource
    private CustomerService customerService;
    @Resource
    private UserService userService;

    @RequestMapping(value="/login", method= RequestMethod.POST)
    @ResponseBody
    public ModelAndView login(UserDomain userDomain,HttpServletRequest request){
        UserDomain user = customerService.validateLogin(userDomain);
        
        if(user.equals("")){
        	ModelAndView modelAndView1 = new ModelAndView("error");
        	return modelAndView1;
        }else{
        	ModelAndView modelAndView = new ModelAndView();
        	request.getSession().setAttribute("user", user);
        	modelAndView.setViewName("redirect:/index");
        	return modelAndView;
        }
    }
    
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView addUser(UserDomain userDomain){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirect:/index");
    	logger.info("=========admin: account={}============", userDomain.getAccount());
        userService.addUser(userDomain);
        return modelAndView;
    }
    
    
    @RequestMapping(value="/list", method= RequestMethod.POST)
    @ResponseBody
    public List<UserDomain> list(UserDomain userDomain){
        logger.info("==================================================account={},password={}", userDomain.getAccount(), userDomain.getPassword());
        return customerService.list(userDomain);
    }
    
    
    
    
    
}
